<?php
include 'adodb5/adodb.inc.php';

class Link {
	private $db;
	
	function __construct() {
		$this->db = ADONewConnection('mysql');
		$this->db->Connect("localhost", "root", "root", "links");
	}
	
	function add($title, $url, $category, $description = "") {
		$this->db->Execute("INSERT INTO `links` (title, url, category_id, description) VALUES ('$title', '$url', '$category', '$description')");
	}
	
	function edit($id, $title= "", $url="", $category="", $description = "") {
		$sql = "UPDATE links SET ";
		if(!empty($title)) {
			$sql .= "title='{$title}', ";
		}
		if(!empty($url)) {
			$sql .= "url='{$url}', ";
		}
		if(!empty($category)) {
			$sql .= "category_id='{$category}', ";
		}
		if(!empty($description)) {
			$sql .= "description='{$description}' ";
		}
		$sql .= "WHERE link_id='{$id}'";
		echo $sql;
		$this->db->Execute($sql);
		
	}
	
	function delete ($id) {
		$this->db->Execute("SELECT * FROM links WHERE link_id='{$id}'");
	}
	
	function view ($id) {
		$result = $this->db->GetRow("SELECT * FROM links WHERE link_id='{$id}'");
		return $result;
	}
	
	function find_id_by_title($title) {
		$result = $this->db->GetRow("SELECT link_id FROM links WHERE title='{$title}'");
		return $result['link_id'];
	}
	
	function find_id_by_url($url) {
		$result = $this->db->GetRow("SELECT link_id FROM links WHERE url='{$url}'");
		return $result['link_id'];
	}
}

	// $link->add("title", "url", "1", "description here");
	// print_r($link->view(23));
	// $link23 = $link->view(23);
	// print $link23['title'];
/*	 print $link->find_id_by_title("Facebook");
	echo "<p />";
	 print $link->find_id_by_url("http://facebook.com");
	echo "<p />"; */
	// $link->edit("30", "titlez", "urlz", "1", "htasdfsadfs");
	?>